## Paper: "Politically Heterogeneous Acquaintances and Close Relationships 
###        are Widespread, and Relate to Lower Partisan Animosity"
## Replication data: Cleaning all datasets
## May, 2025

## Clear workspace in RStudio
rm(list=ls())
  
## Load packages 
library(readxl)
library(tidyverse)
library(texreg) 
library(stargazer)
library(lmtest) 
library(moments) 
library(estimatr) 
library(readxl) 
library(lubridate)
library(scales) 
library(ggplot2) 
library(magrittr)
library(ggeffects)
library(stargazer)
install.packages("extrafont")
library(extrafont) 
library(writexl)
install.packages("vtable")
library(vtable)
install.packages("effsize")
library(effsize)
install.packages("qualtRics") 
library(qualtRics)
install.packages("haven")
library(haven)
install.packages("devtools") 
library(devtools) 
devtools::install_github("leeper/cregg")  
library(cregg)
install.packages("tidytext")
library(tidytext)
install.packages("stringr")
library(stringr)
library(Rmisc)
library(grid)
library(gridExtra)
library(cowplot)
library(xtable)
library(broom)

setwd("C:/Users/jonaf/Documents/Documents/EUI/Working Papers/US_Paper/Submission/Soc_Science/Replication_data")


#### Load 'data1'

library(readxl)
data <- read_excel("data1.xlsx") 


### Exclusion

# Delete first two rows

data <- data[-c(1, 2),]

# Limit data to those who finished the survey (22 didn't)

data$finished <- as.factor(data$Finished)

data <- data %>% filter(finished == "True")

# Throw out people who don't complete the attention checks

data <- data %>% filter(attention_check_1 == "Red,Green")

data <- data %>% filter(attention_check_2_2 == "Neither agree nor disagree")

# Throw out duplicates in IP addresses

data <- data %>% group_by(data$IPAddress) %>%
  filter(n() == 1) %>% 
  ungroup()

### Transform variables and controls

## Make relevant variables into numeric and factor variables

data <- data %>% 
  mutate_at(c('feeling_therm_1', 'feeling_therm_2', 'feeling_therm_3', 
              'feeling_therm_4'), as.numeric)

data <- data %>% 
  mutate_at(c('partisanship', 'gender', 'lean',
              'strong_rep', 'strong_dem', 'a1partisanship', 
              'a2partisanship','a3partisanship', 'a4partisanship', 
              'a5partisanship', 'ard_reps_morefewer10', 'ard_dems_morefewer10',
              'ard_reps_more10', 'ard_reps_fewer10', 
              'ard_dems_more10', 'ard_dems_fewer10', 'polinterest',
              'race', 'hispanic',
              'education', 'religion', 'puzzle', 'puzzle.1',
              'puzzle_DO', 'puzzle_DO.1', 'ideology', 'state', 'a1talkpolitics',
              'a2talkpolitics', 'a3talkingpolitics', 'a4talkingpolitics',
              'a5talkingpolitics'), as.factor)

## Age was an open text box, so some people filled in strange things -> make NA

data$age[data$age == "Oklahoma"] <- NA
data$age[data$age == ",34"] <- 34
data$age[data$age == "+86"] <- NA
data$age[data$age == "1000"] <- NA
data$age[data$age == "53716"] <- NA
data$age[data$age == "27830"] <- NA
data$age[data$age == "29803"] <- NA
data$age[data$age == "78108"] <- NA
data$age[data$age == "30021"] <- NA
data$age[data$age == "343"] <- NA
data$age[data$age == "Yw"] <- NA
data$age[data$age == "636"] <- NA

data$age <- as.numeric(data$age)
data$Age <- data$age

# Education: binary: some college or more versus the rest (ref) 

data$Education <- ifelse(data$education == "College degree" | data$education == "Some graduate school" |
                            data$education == "Graduate school" | data$education == "Some college", 
                         "Some College", "No College")

data$Education <- as.factor(data$Education)
data$Education <- relevel(data$Education, ref = "No College")

# Race

data$Race <- NA
data$Race[data$race == "White" & data$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic White"
data$Race[data$race == "Asian" & data$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic Asian"
data$Race[data$race == "Black or African American" & data$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic Black"
data$Race[data$race == "American Indian or Alaska Native" | 
            data$race == "Native Hawaiian or Pacific Islander" |
            data$race == "Some other race" 
          & data$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic Some Other race"
data$Race[data$hispanic == "Hispanic or Latino"] <- "Hispanic any race"

data$Race <- as.factor(data$Race)
data$Race <- relevel(data$Race, ref = "non-Hispanic White")

## Make new variable for partisanship, that includes leaners

data$all_partisanship <- NA
data$all_partisanship[data$partisanship == "Democrat" | data$lean == "Closer to the Democratic party"] <- "Democrat"
data$all_partisanship[data$partisanship == "Republican" | data$lean == "Closer to the Republican party"] <- "Republican"

# Make partisanship variable that distinguishes between dems and close dems

data$partisanship_detailed <- NA
data$partisanship_detailed[data$partisanship == "Democrat"] <- "Democrat" 
data$partisanship_detailed[data$partisanship == "Republican"] <- "Republican" 
data$partisanship_detailed[data$lean == "Closer to the Democratic party"] <- "Close dem"
data$partisanship_detailed[data$lean == "Closer to the Republican party"] <- "Close rep"

# Dummy for strong partisanship

data$strong_partisan <- ifelse((data$strong_rep == "Strong" |
                                 data$strong_dem == "Strong"), 1, 0)

data$strong_partisan[is.na(data$strong_partisan)] <- 0

data$Strong_partisan <- data$strong_partisan

## Ideology
data$ideology[data$ideology == ""] <-  NA
data$ideology <- factor(data$ideology, levels = c("Slightly liberal", "Liberal", "Extremely liberal",
                                        "Moderate; middle of the road", "Slightly conservative", "Conservative",
                                        "Extremely conservative"))

data$ideology <- relevel(data$ideology, ref = "Moderate; middle of the road")
data$Ideology <- data$ideology

#polinterest

data$polinterest[data$polinterest == ""] <-  NA
data$Political_interest <- as.numeric(data$polinterest)

# gender
data$Gender <- ifelse(data$gender == "Male", "Male", "Female/non-Binary")
data$Gender <- as.factor(data$Gender)
data$Gender <- relevel(data$Gender, ref = "Female/non-Binary")



###### 
###### Independent and Dependent variable construction
######

### Independent variables

##
## Close-tie heterogeneity: binary variable that denotes whether you have at least
## one out-partisan in your network
##

## First check how many ties people register on average

# Fill alterlists with NA's, because now they're left blank

data["AlterList_1"][data["AlterList_1"] == ''] <- NA
data["AlterList_2"][data["AlterList_2"] == ''] <- NA
data["AlterList_3"][data["AlterList_3"] == ''] <- NA
data["AlterList_4"][data["AlterList_4"] == ''] <- NA
data["AlterList_5"][data["AlterList_5"] == ''] <- NA

# Make dummy for whether there is a tie

data$a1tie <- ifelse(is.na(data$AlterList_1), 0, 1)
data$a2tie <- ifelse(is.na(data$AlterList_2), 0, 1)
data$a3tie <- ifelse(is.na(data$AlterList_3), 0, 1)
data$a4tie <- ifelse(is.na(data$AlterList_4), 0, 1)
data$a5tie <- ifelse(is.na(data$AlterList_5), 0, 1)

# Make a variable for the total number of ties people have

data <- data %>%
  mutate(total_ties = a1tie + a2tie + a3tie + a4tie + a5tie)

## Variable for out-partisan ties

#a1
data <- data %>% mutate(a1outparty = case_when(
  all_partisanship == "Democrat" & a1partisanship == "Republican" ~ 1,
  all_partisanship == "Republican" & a1partisanship == "Democrat" ~ 1,
  all_partisanship == "Democrat" & a1partisanship == "Democrat" ~ 0,
  all_partisanship == "Republican" & a1partisanship == "Republican" ~ 0,
))

#a2
data <- data %>% mutate(a2outparty = case_when(
  all_partisanship == "Democrat" & a2partisanship == "Republican" ~ 1,
  all_partisanship == "Republican" & a2partisanship == "Democrat" ~ 1,
  all_partisanship == "Democrat" & a2partisanship == "Democrat" ~ 0,
  all_partisanship == "Republican" & a2partisanship == "Republican" ~ 0,
))

# a3
data <- data %>% mutate(a3outparty = case_when(
  all_partisanship == "Democrat" & a3partisanship == "Republican" ~ 1,
  all_partisanship == "Republican" & a3partisanship == "Democrat" ~ 1,
  all_partisanship == "Democrat" & a3partisanship == "Democrat" ~ 0,
  all_partisanship == "Republican" & a3partisanship == "Republican" ~ 0,
))

# a4
data <- data %>% mutate(a4outparty = case_when(
  all_partisanship == "Democrat" & a4partisanship == "Republican" ~ 1,
  all_partisanship == "Republican" & a4partisanship == "Democrat" ~ 1,
  all_partisanship == "Democrat" & a4partisanship == "Democrat" ~ 0,
  all_partisanship == "Republican" & a4partisanship == "Republican" ~ 0,
))

# a5
data <- data %>% mutate(a5outparty = case_when(
  all_partisanship == "Democrat" & a5partisanship == "Republican" ~ 1,
  all_partisanship == "Republican" & a5partisanship == "Democrat" ~ 1,
  all_partisanship == "Democrat" & a5partisanship == "Democrat" ~ 0,
  all_partisanship == "Republican" & a5partisanship == "Republican" ~ 0,
))

# Close network heterogeneity variable

data$close_network <- NA
data$close_network[data$a1outparty == "0" | data$a2outparty == "0" | 
                           data$a3outparty == "0" | data$a4outparty == "0" | 
                           data$a5outparty == "0"] <- 0
data$close_network[data$a1outparty == "1" | data$a2outparty == "1" | 
                           data$a3outparty == "1" | data$a4outparty == "1" | 
                           data$a5outparty == "1"] <- 1

data$close_network <- as.factor(data$close_network)
levels(data$close_network) <- c("Homogeneous", "Heterogeneous")

# For robustness, make a variable that counts the number of outpartisan ties

data$close_network_numeric <- apply(data[, c("a1outparty", "a2outparty", "a3outparty",
                                             "a4outparty", "a5outparty")], 1, 
                                    function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE))

data$close_network_numeric[data$close_network_numeric == "5"] <- NA

#### Talking politics

# Recode talking politics variable (1 - yes, 0 is no, don't know or not filled in is NA)

data$a1talkpolitics[data$a1talkpolitics == "Don't know" | data$a1talkpolitics == ""] <- NA

data$a1talkpolitics <- ifelse(data$a1talkpolitics == "Yes", 1, 0)

data$a2talkpolitics[data$a2talkpolitics == "Don't know" | data$a2talkpolitics == ""] <- NA

data$a2talkpolitics <- ifelse(data$a2talkpolitics == "Yes", 1, 0)

data$a3talkingpolitics[data$a3talkingpolitics == "Don't know" | data$a3talkingpolitics == ""] <- NA

data$a3talkingpolitics <- ifelse(data$a3talkingpolitics == "Yes", 1, 0)

data$a4talkingpolitics[data$a3talkingpolitics == "Don't know" | data$a4talkingpolitics == ""] <- NA

data$a4talkingpolitics <- ifelse(data$a4talkingpolitics == "Yes", 1, 0)

data$a5talkingpolitics[data$a5talkingpolitics == "Don't know" | data$a5talkingpolitics == ""] <- NA

data$a5talkingpolitics <- ifelse(data$a5talkingpolitics == "Yes", 1, 0)

## Talk politics total (irrespective of inparty or outparty tie)

data$talking_politics_total <- apply(data[, c("a1talkpolitics", "a2talkpolitics", "a3talkingpolitics",
                                             "a4talkingpolitics", "a5talkingpolitics")], 1, 
                                    function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE))

## Make variable for total talking politics with outparty ties

data$a1outparty_talkpolitics <- ifelse(data$a1outparty == "0" | data$a1outparty == "NA", NA, data$a1talkpolitics)
data$a2outparty_talkpolitics <- ifelse(data$a2outparty == "0" | data$a2outparty == "NA", NA, data$a2talkpolitics)
data$a3outparty_talkpolitics <- ifelse(data$a3outparty == "0" | data$a3outparty == "NA", NA, data$a3talkingpolitics)
data$a4outparty_talkpolitics <- ifelse(data$a4outparty == "0" | data$a4outparty == "NA", NA, data$a4talkingpolitics)
data$a5outparty_talkpolitics <- ifelse(data$a5outparty == "0" | data$a5outparty == "NA", NA, data$a5talkingpolitics)

data$talking_outparty_total <- apply(data[, c("a1outparty_talkpolitics", "a2outparty_talkpolitics", "a3outparty_talkpolitics",
                                              "a4outparty_talkpolitics", "a5outparty_talkpolitics")], 1, 
                                     function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE))

# make dummy
data$outparty_talkpolitics_dummy <- NA
data$outparty_talkpolitics_dummy[data$a1outparty_talkpolitics == "0" & data$a1outparty_talkpolitics != "1"| 
                                   data$a2outparty_talkpolitics == "0" & data$a2outparty_talkpolitics != "1" |
                                   data$a3outparty_talkpolitics == "0" & data$a3outparty_talkpolitics != "1" | 
                                   data$a4outparty_talkpolitics == "0" & data$a4outparty_talkpolitics != "1" |
                                   data$a5outparty_talkpolitics == "0" & data$a5outparty_talkpolitics != "1"] <- 0
data$outparty_talkpolitics_dummy[data$a1outparty_talkpolitics == "1" | data$a2outparty_talkpolitics == "1" |
                                   data$a3outparty_talkpolitics == "1" | data$a4outparty_talkpolitics == "1" |
                                   data$a5outparty_talkpolitics == "1"] <- 1

data$outparty_talkpolitics_dummy <- as.factor(data$outparty_talkpolitics_dummy)


# Total talking politics with inparty tie

data$a1inparty_talkpolitics <- ifelse(data$a1outparty == "1" | data$a1outparty == "NA", NA, data$a1talkpolitics)
data$a2inparty_talkpolitics <- ifelse(data$a2outparty == "1" | data$a2outparty == "NA", NA, data$a2talkpolitics)
data$a3inparty_talkpolitics <- ifelse(data$a3outparty == "1" | data$a3outparty == "NA", NA, data$a3talkingpolitics)
data$a4inparty_talkpolitics <- ifelse(data$a4outparty == "1" | data$a4outparty == "NA", NA, data$a4talkingpolitics)
data$a5inparty_talkpolitics <- ifelse(data$a5outparty == "1" | data$a5outparty == "NA", NA, data$a5talkingpolitics)


data$talking_inparty_total <- apply(data[, c("a1inparty_talkpolitics", "a2inparty_talkpolitics", "a3inparty_talkpolitics",
                                              "a4inparty_talkpolitics", "a5inparty_talkpolitics")], 1, 
                                     function(x) if (all(is.na(x))) NA else sum(x, na.rm = TRUE))

##
## Acquaintance network heterogeneity (ARD): 
# numerical variable for the amount of outpartisans people say they know
##

# transform categories into numbers (take lower bound of the categories)

data$dems_fewer10 <- NA
data$dems_fewer10[data$ard_dems_fewer10 == "0"] <- 0
data$dems_fewer10[data$ard_dems_fewer10 == "1-2"] <- 1
data$dems_fewer10[data$ard_dems_fewer10 == "3-5"] <- 3
data$dems_fewer10[data$ard_dems_fewer10 == "6-9"] <- 6

data$dems_more10 <- NA
data$dems_more10[data$ard_dems_more10 == "10-12"] <- 10
data$dems_more10[data$ard_dems_more10 == "13-20"] <- 13
data$dems_more10[data$ard_dems_more10 == "21-50"] <- 21
data$dems_more10[data$ard_dems_more10 == "more than 50"] <- 50

data$reps_fewer10 <- NA
data$reps_fewer10[data$ard_reps_fewer10 == "0"] <- 0
data$reps_fewer10[data$ard_reps_fewer10 == "1 - 2"] <- 1
data$reps_fewer10[data$ard_reps_fewer10 == "3 - 5"] <- 3
data$reps_fewer10[data$ard_reps_fewer10 == "6 - 9"] <- 6

data$reps_more10 <- NA
data$reps_more10[data$ard_reps_more10 == "10 - 12"] <- 10
data$reps_more10[data$ard_reps_more10 == "13 - 20"] <- 13
data$reps_more10[data$ard_reps_more10 == "21 - 50"] <- 21
data$reps_more10[data$ard_reps_more10 == "more than 50"] <- 50

# Make new variable for all reps and all dems

data$ardLB_allreps <- ifelse(is.na(data$reps_fewer10) & is.na(data$reps_more10), 
                           NA, 
                           rowSums(data[, c("reps_fewer10", "reps_more10")], na.rm = TRUE))


data$ardLB_alldems <- ifelse(is.na(data$dems_fewer10) & is.na(data$dems_more10), 
                           NA, 
                           rowSums(data[, c("dems_fewer10", "dems_more10")], na.rm = TRUE))

# Make variable of outparty ties

data$ardLB_outpartyties <- NA
data$ardLB_outpartyties[data$all_partisanship == "Democrat"] <- 
  data$ardLB_allreps[data$all_partisanship == "Democrat"]
data$ardLB_outpartyties[data$all_partisanship == "Republican"] <- 
  data$ardLB_alldems[data$all_partisanship == "Republican"]
data$ardLB_outpartyties[data$ard_reps_fewer10 == "0" & 
                        data$ard_dems_fewer10 == "0"] <- NA

data$ardLB_inpartyties <- NA
data$ardLB_inpartyties[data$all_partisanship == "Democrat"] <- 
  data$ardLB_alldems[data$all_partisanship == "Democrat"]
data$ardLB_inpartyties[data$all_partisanship == "Republican"] <- 
  data$ardLB_allreps[data$all_partisanship == "Republican"]
data$ardLB_inpartyties[data$ard_reps_fewer10 == "0" & 
                       data$ard_dems_fewer10 == "0"] <- NA

# Make variable of whole network

data$ardLB_wholenetwork <- ifelse(is.na(data$ardLB_allreps) & is.na(data$ardLB_alldems),
                                NA,
                                rowSums(data[,c("ardLB_allreps", "ardLB_alldems")], na.rm = TRUE)) 

# Make ard variable: proportion of outparty ties

data$ardLB_including_isolated_people <- ifelse(data$ardLB_outpartyties == 0 & data$ardLB_wholenetwork == 0, 999,
                                             ifelse(data$ardLB_inpartyties == 0 & data$ardLB_outpartyties > 0, 1,
                                                    ifelse(data$ardLB_outpartyties == 0 & data$ardLB_inpartyties > 0, 0,
                                                           data$ardLB_outpartyties / data$ardLB_wholenetwork)))

data$ard_LB <- ifelse(data$ardLB_outpartyties == 0 & data$ardLB_wholenetwork == 0, NA,
                   ifelse(data$ardLB_inpartyties == 0 & data$ardLB_outpartyties > 0, 1,
                          ifelse(data$ardLB_outpartyties == 0 & data$ardLB_inpartyties > 0, 0,
                                 data$ardLB_outpartyties / data$ardLB_wholenetwork)))

data$Acquaintance_heterogeneity <- data$ard_LB 



# Make dummy for graphs

data$Acquaintance_dummy <- ifelse(data$Acquaintance_heterogeneity > 0.33, 1, 0) 

data$Acquaintance_dummy <- as.factor(data$Acquaintance_dummy)

levels(data$Acquaintance_dummy) <- c("Homogeneous", "Heterogeneous")

data$ardLB_outpartyties_dummy <- ifelse(data$ardLB_outpartyties > 3, 1, 0) 

########
######## Dependent variables
########

## 1.1 Feeling thermometer: outparty

data$outparty_affect <- NA

data$outparty_affect[data$all_partisanship == "Democrat"] <- data$feeling_therm_2[data$all_partisanship == "Democrat"]  
data$outparty_affect[data$all_partisanship == "Republican"] <- data$feeling_therm_1[data$all_partisanship == "Republican"]  

data$outpartisan_affect <- NA

data$outpartisan_affect[data$all_partisanship == "Democrat"] <- data$feeling_therm_4[data$all_partisanship == "Democrat"]  
data$outpartisan_affect[data$all_partisanship == "Republican"] <- data$feeling_therm_3[data$all_partisanship == "Republican"]  

## 1.2 Feeling thermometer: inparty

data$inparty_affect <- NA

data$inparty_affect[data$all_partisanship == "Democrat"] <- data$feeling_therm_1[data$all_partisanship == "Democrat"]  
data$inparty_affect[data$all_partisanship == "Republican"] <- data$feeling_therm_2[data$all_partisanship == "Republican"]  

data$inpartisan_affect <- NA

data$inpartisan_affect[data$all_partisanship == "Democrat"] <- data$feeling_therm_3[data$all_partisanship == "Democrat"]  
data$inpartisan_affect[data$all_partisanship == "Republican"] <- data$feeling_therm_4[data$all_partisanship == "Republican"]  

### 2. Trait ratings

# Inparty trait rating

data$inparty_american <- NA
data$inparty_american[data$all_partisanship == "Democrat"] <- data$trait_dems_1[data$all_partisanship == "Democrat"] 
data$inparty_american[data$all_partisanship == "Republican"] <- data$trait_reps_1[data$all_partisanship == "Republican"] 

data$inparty_intelligent <- NA
data$inparty_intelligent[data$all_partisanship == "Democrat"] <- data$trait_dems_2[data$all_partisanship == "Democrat"] 
data$inparty_intelligent[data$all_partisanship == "Republican"] <- data$trait_reps_2[data$all_partisanship == "Republican"] 

data$inparty_honest <- NA
data$inparty_honest[data$all_partisanship == "Democrat"] <- data$trait_dems_3[data$all_partisanship == "Democrat"] 
data$inparty_honest[data$all_partisanship == "Republican"] <- data$trait_reps_3[data$all_partisanship == "Republican"] 

data$inparty_openminded <- NA
data$inparty_openminded[data$all_partisanship == "Democrat"] <- data$trait_dems_4[data$all_partisanship == "Democrat"] 
data$inparty_openminded[data$all_partisanship == "Republican"] <- data$trait_reps_4[data$all_partisanship == "Republican"] 

data$inparty_generous <- NA
data$inparty_generous[data$all_partisanship == "Democrat"] <- data$trait_dems_5[data$all_partisanship == "Democrat"] 
data$inparty_generous[data$all_partisanship == "Republican"] <- data$trait_reps_5[data$all_partisanship == "Republican"] 

data$inparty_hypocritical <- NA
data$inparty_hypocritical[data$all_partisanship == "Democrat"] <- data$trait_dems_6[data$all_partisanship == "Democrat"] 
data$inparty_hypocritical[data$all_partisanship == "Republican"] <- data$trait_reps_6[data$all_partisanship == "Republican"] 

data$inparty_selfish <- NA
data$inparty_selfish[data$all_partisanship == "Democrat"] <- data$trait_dems_7[data$all_partisanship == "Democrat"] 
data$inparty_selfish[data$all_partisanship == "Republican"] <- data$trait_reps_7[data$all_partisanship == "Republican"] 

data$inparty_mean <- NA
data$inparty_mean[data$all_partisanship == "Democrat"] <- data$trait_dems_8[data$all_partisanship == "Democrat"] 
data$inparty_mean[data$all_partisanship == "Republican"] <- data$trait_reps_8[data$all_partisanship == "Republican"] 

# Outparty trait rating

data$outparty_american <- NA
data$outparty_american[data$all_partisanship == "Democrat"] <- data$trait_reps_1[data$all_partisanship == "Democrat"] 
data$outparty_american[data$all_partisanship == "Republican"] <- data$trait_dems_1[data$all_partisanship == "Republican"] 


data$outparty_intelligent <- NA
data$outparty_intelligent[data$all_partisanship == "Democrat"] <- data$trait_reps_2[data$all_partisanship == "Democrat"] 
data$outparty_intelligent[data$all_partisanship == "Republican"] <- data$trait_dems_2[data$all_partisanship == "Republican"] 

data$outparty_honest <- NA
data$outparty_honest[data$all_partisanship == "Democrat"] <- data$trait_reps_3[data$all_partisanship == "Democrat"] 
data$outparty_honest[data$all_partisanship == "Republican"] <- data$trait_dems_3[data$all_partisanship == "Republican"] 

data$outparty_openminded <- NA
data$outparty_openminded[data$all_partisanship == "Democrat"] <- data$trait_reps_4[data$all_partisanship == "Democrat"] 
data$outparty_openminded[data$all_partisanship == "Republican"] <- data$trait_dems_4[data$all_partisanship == "Republican"] 

data$outparty_generous <- NA
data$outparty_generous[data$all_partisanship == "Democrat"] <- data$trait_reps_5[data$all_partisanship == "Democrat"] 
data$outparty_generous[data$all_partisanship == "Republican"] <- data$trait_dems_5[data$all_partisanship == "Republican"] 

data$outparty_hypocritical <- NA
data$outparty_hypocritical[data$all_partisanship == "Democrat"] <- data$trait_reps_6[data$all_partisanship == "Democrat"] 
data$outparty_hypocritical[data$all_partisanship == "Republican"] <- data$trait_dems_6[data$all_partisanship == "Republican"] 

data$outparty_selfish <- NA
data$outparty_selfish[data$all_partisanship == "Democrat"] <- data$trait_reps_7[data$all_partisanship == "Democrat"] 
data$outparty_selfish[data$all_partisanship == "Republican"] <- data$trait_dems_7[data$all_partisanship == "Republican"] 

data$outparty_mean <- NA
data$outparty_mean[data$all_partisanship == "Democrat"] <- data$trait_reps_8[data$all_partisanship == "Democrat"] 
data$outparty_mean[data$all_partisanship == "Republican"] <- data$trait_dems_8[data$all_partisanship == "Republican"] 

## Recode all to make numerical and to have positive values denote a warmer rating

data <- data %>% mutate(inparty_american = case_when(
  inparty_american == "Not at all well" ~ 1,
  inparty_american == "Not too well" ~ 2,
  inparty_american == "Somewhat well" ~ 3,
  inparty_american == "Very well" ~ 4,
  inparty_american == "Extremely well" ~ 5))

data <- data %>% mutate(inparty_intelligent = case_when(
  inparty_intelligent == "Not at all well" ~ 1,
  inparty_intelligent == "Not too well" ~ 2,
  inparty_intelligent == "Somewhat well" ~ 3,
  inparty_intelligent == "Very well" ~ 4,
  inparty_intelligent == "Extremely well" ~ 5))

data <- data %>% mutate(inparty_honest = case_when(
  inparty_honest == "Not at all well" ~ 1,
  inparty_honest == "Not too well" ~ 2,
  inparty_honest == "Somewhat well" ~ 3,
  inparty_honest == "Very well" ~ 4,
  inparty_honest == "Extremely well" ~ 5))

data <- data %>% mutate(inparty_openminded = case_when(
  inparty_openminded == "Not at all well" ~ 1,
  inparty_openminded == "Not too well" ~ 2,
  inparty_openminded == "Somewhat well" ~ 3,
  inparty_openminded == "Very well" ~ 4,
  inparty_openminded == "Extremely well" ~ 5))

data <- data %>% mutate(inparty_generous = case_when(
  inparty_generous == "Not at all well" ~ 1,
  inparty_generous == "Not too well" ~ 2,
  inparty_generous == "Somewhat well" ~ 3,
  inparty_generous == "Very well" ~ 4,
  inparty_generous == "Extremely well" ~ 5))

data <- data %>% mutate(inparty_hypocritical = case_when(
  inparty_hypocritical == "Not at all well" ~ 5,
  inparty_hypocritical == "Not too well" ~ 4,
  inparty_hypocritical == "Somewhat well" ~ 3,
  inparty_hypocritical == "Very well" ~ 2,
  inparty_hypocritical == "Extremely well" ~ 1))

data <- data %>% mutate(inparty_selfish = case_when(
  inparty_selfish == "Not at all well" ~ 5,
  inparty_selfish == "Not too well" ~ 4,
  inparty_selfish == "Somewhat well" ~ 3,
  inparty_selfish == "Very well" ~ 2,
  inparty_selfish == "Extremely well" ~ 1))

data <- data %>% mutate(inparty_mean = case_when(
  inparty_mean == "Not at all well" ~ 5,
  inparty_mean == "Not too well" ~ 4,
  inparty_mean == "Somewhat well" ~ 3,
  inparty_mean == "Very well" ~ 2,
  inparty_mean == "Extremely well" ~ 1))

data <- data %>% mutate(outparty_american = case_when(
  outparty_american == "Not at all well" ~ 1,
  outparty_american == "Not too well" ~ 2,
  outparty_american == "Somewhat well" ~ 3,
  outparty_american == "Very well" ~ 4,
  outparty_american == "Extremely well" ~ 5))

data <- data %>% mutate(outparty_intelligent = case_when(
  outparty_intelligent == "Not at all well" ~ 1,
  outparty_intelligent == "Not too well" ~ 2,
  outparty_intelligent == "Somewhat well" ~ 3,
  outparty_intelligent == "Very well" ~ 4,
  outparty_intelligent == "Extremely well" ~ 5))

data <- data %>% mutate(outparty_honest = case_when(
  outparty_honest == "Not at all well" ~ 1,
  outparty_honest == "Not too well" ~ 2,
  outparty_honest == "Somewhat well" ~ 3,
  outparty_honest == "Very well" ~ 4,
  outparty_honest == "Extremely well" ~ 5))

data <- data %>% mutate(outparty_openminded = case_when(
  outparty_openminded == "Not at all well" ~ 1,
  outparty_openminded == "Not too well" ~ 2,
  outparty_openminded == "Somewhat well" ~ 3,
  outparty_openminded == "Very well" ~ 4,
  outparty_openminded == "Extremely well" ~ 5))

data <- data %>% mutate(outparty_generous = case_when(
  outparty_generous == "Not at all well" ~ 1,
  outparty_generous == "Not too well" ~ 2,
  outparty_generous == "Somewhat well" ~ 3,
  outparty_generous == "Very well" ~ 4,
  outparty_generous == "Extremely well" ~ 5))

data <- data %>% mutate(outparty_hypocritical = case_when(
  outparty_hypocritical == "Not at all well" ~ 5,
  outparty_hypocritical == "Not too well" ~ 4,
  outparty_hypocritical == "Somewhat well" ~ 3,
  outparty_hypocritical == "Very well" ~ 2,
  outparty_hypocritical == "Extremely well" ~ 1))

data <- data %>% mutate(outparty_selfish = case_when(
  outparty_selfish == "Not at all well" ~ 5,
  outparty_selfish == "Not too well" ~ 4,
  outparty_selfish == "Somewhat well" ~ 3,
  outparty_selfish == "Very well" ~ 2,
  outparty_selfish == "Extremely well" ~ 1))

data <- data %>% mutate(outparty_mean = case_when(
  outparty_mean == "Not at all well" ~ 5,
  outparty_mean == "Not too well" ~ 4,
  outparty_mean == "Somewhat well" ~ 3,
  outparty_mean == "Very well" ~ 2,
  outparty_mean == "Extremely well" ~ 1))

## 3 Trust Item 

data$inparty_trust <- NA
data$inparty_trust[data$all_partisanship == "Democrat"] <- data$trust_dems[data$all_partisanship == "Democrat"] 
data$inparty_trust[data$all_partisanship == "Republican"] <- data$trust_reps[data$all_partisanship == "Republican"] 

data$outparty_trust <- NA
data$outparty_trust[data$all_partisanship == "Democrat"] <- data$trust_reps[data$all_partisanship == "Democrat"] 
data$outparty_trust[data$all_partisanship == "Republican"] <- data$trust_dems[data$all_partisanship == "Republican"] 

data <- data %>% mutate(inparty_trust = case_when(
  inparty_trust == "Almost always" ~ 5,
  inparty_trust == "Most of the time" ~ 4,
  inparty_trust == "About half the time" ~ 3,
  inparty_trust == "Once in a while" ~ 2,
  inparty_trust == "Almost never" ~ 1))

data <- data %>% mutate(outparty_trust = case_when(
  outparty_trust == "Almost always" ~ 5,
  outparty_trust == "Most of the time" ~ 4,
  outparty_trust == "About half the time" ~ 3,
  outparty_trust == "Once in a while" ~ 2,
  outparty_trust == "Almost never" ~ 1))

## Combine thermometer, traits and trust into one scale (0-1) for outparty feelings

# Ingroup

data <- data %>% mutate(inpartisan_affect = (inpartisan_affect/100))
data <- data %>% mutate(inparty_affect = (inparty_affect/100))
data <- data %>% mutate(inparty_american = (inparty_american-1)/(5-1))
data <- data %>% mutate(inparty_intelligent = (inparty_intelligent-1)/(5-1))
data <- data %>% mutate(inparty_honest = (inparty_honest-1)/(5-1))
data <- data %>% mutate(inparty_openminded = (inparty_openminded-1)/(5-1))
data <- data %>% mutate(inparty_generous = (inparty_generous-1)/(5-1))
data <- data %>% mutate(inparty_hypocritical = (inparty_hypocritical-1)/(5-1))
data <- data %>% mutate(inparty_selfish = (inparty_selfish-1)/(5-1))
data <- data %>% mutate(inparty_mean = (inparty_mean-1)/(5-1))
data <- data %>% mutate(inparty_trust = (inparty_trust-1)/(5-1))

data <- data %>% 
  mutate(inparty_final = inpartisan_affect + inparty_american + inparty_intelligent +
                             inparty_honest + inparty_openminded + inparty_generous +
                             inparty_hypocritical + inparty_selfish + inparty_mean +
                             inparty_trust)

data <- data %>% 
  mutate(inparty_final = inparty_final/10)

# Outgroup

data <- data %>% mutate(outpartisan_affect = (outpartisan_affect/100))
data <- data %>% mutate(outparty_affect = (outparty_affect/100))
data <- data %>% mutate(outparty_american = (outparty_american-1)/(5-1))
data <- data %>% mutate(outparty_intelligent = (outparty_intelligent-1)/(5-1))
data <- data %>% mutate(outparty_honest = (outparty_honest-1)/(5-1))
data <- data %>% mutate(outparty_openminded = (outparty_openminded-1)/(5-1))
data <- data %>% mutate(outparty_generous = (outparty_generous-1)/(5-1))
data <- data %>% mutate(outparty_hypocritical = (outparty_hypocritical-1)/(5-1))
data <- data %>% mutate(outparty_selfish = (outparty_selfish-1)/(5-1))
data <- data %>% mutate(outparty_mean = (outparty_mean-1)/(5-1))
data <- data %>% mutate(outparty_trust = (outparty_trust-1)/(5-1))

data <- data %>% 
  mutate(outparty_final = outpartisan_affect + outparty_american + outparty_intelligent +
                              outparty_honest + outparty_openminded + outparty_generous +
                              outparty_hypocritical + outparty_selfish + outparty_mean + 
                              outparty_trust)

data <- data %>% 
  mutate(outparty_final = outparty_final/10)

# Dummy

data$outparty_final_dummy <- ifelse(data$outparty_final < 0.48, 1, 0)

data <- data %>% 
  mutate(outparty_traits = ((outparty_american + outparty_intelligent +
                              outparty_honest + outparty_openminded + outparty_generous +
                              outparty_hypocritical + outparty_selfish) * 5) / 7)

# Robustness: Inparty - outparty measure

data <- data %>%
  mutate(AP_final = ((inpartisan_affect - outpartisan_affect) + (inparty_american - outparty_american) +
                       (inparty_intelligent - outparty_intelligent) + (inparty_honest - outparty_honest) +
                       (inparty_openminded - outparty_openminded) + (inparty_generous - outparty_generous) +
                       (inparty_hypocritical - outparty_hypocritical) + (inparty_selfish - outparty_selfish) +
                       (inparty_mean - outparty_mean) + (inparty_trust - outparty_trust))/10) 


#######
####### Logic Puzzle
#######  

# Treatment variable: presented with copartisan partner (0), or outpartisan partner (1)
# The puzzle_DO (for reps) and puzzle_DO.1 (for dems) show the two choices 
# they're offered, which I use to code the treatment. 

data$treatment_puzzle <- NA

data$treatment_puzzle[data$partisanship == "Republican" & data$puzzle_DO == "John works in a supermarket, is 43, likes to swim, and is a Republican.|James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 0
data$treatment_puzzle[data$lean == "Closer to the Republican party" & data$puzzle_DO == "John works in a supermarket, is 43, likes to swim, and is a Republican.|James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 0

data$treatment_puzzle[data$partisanship == "Republican" & data$puzzle_DO == "John works in a supermarket, is 43, likes to swim, and is a Republican.|James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 1
data$treatment_puzzle[data$lean == "Closer to the Republican party" & data$puzzle_DO == "John works in a supermarket, is 43, likes to swim, and is a Republican.|James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 1

data$treatment_puzzle[data$partisanship == "Democrat" & data$puzzle_DO.1 == "John works in a supermarket, is 43, likes to swim, and is a Democrat.|James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 0
data$treatment_puzzle[data$lean == "Closer to the Democratic party" & data$puzzle_DO.1 == "John works in a supermarket, is 43, likes to swim, and is a Democrat.|James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 0

data$treatment_puzzle[data$partisanship == "Democrat" & data$puzzle_DO.1 == "John works in a supermarket, is 43, likes to swim, and is a Democrat.|James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 1
data$treatment_puzzle[data$lean == "Closer to the Democratic party" & data$puzzle_DO.1 == "John works in a supermarket, is 43, likes to swim, and is a Democrat.|James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 1

# Outcome variable: whether you choose the person who works in the supermarket (0) 
# or the engineer (1). Qualtrics saved 'puzzle' for republicans and 'puzzle.1' 
# for democrats. 

data$outcome_puzzle <- NA
data$outcome_puzzle[data$partisanship == "Republican" & data$puzzle == "John works in a supermarket, is 43, likes to swim, and is a Republican."] <- 0
data$outcome_puzzle[data$partisanship == "Republican" & data$puzzle == "James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 1
data$outcome_puzzle[data$partisanship == "Republican" & data$puzzle == "James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 1

data$outcome_puzzle[data$lean == "Closer to the Republican party" & data$puzzle == "John works in a supermarket, is 43, likes to swim, and is a Republican."] <- 0
data$outcome_puzzle[data$lean == "Closer to the Republican party" & data$puzzle == "James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 1
data$outcome_puzzle[data$lean == "Closer to the Republican party" & data$puzzle == "James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 1

data$outcome_puzzle[data$partisanship == "Democrat" & data$puzzle.1 == "John works in a supermarket, is 43, likes to swim, and is a Democrat."] <- 0
data$outcome_puzzle[data$partisanship == "Democrat" & data$puzzle.1 == "James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 1
data$outcome_puzzle[data$partisanship == "Democrat" & data$puzzle.1 == "James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 1

data$outcome_puzzle[data$lean == "Closer to the Democratic party" & data$puzzle.1 == "John works in a supermarket, is 43, likes to swim, and is a Democrat."] <- 0
data$outcome_puzzle[data$lean == "Closer to the Democratic party" & data$puzzle.1 == "James is an engineer, is 41, likes to go on walks, and is a Republican."] <- 1
data$outcome_puzzle[data$lean == "Closer to the Democratic party" & data$puzzle.1 == "James is an engineer, is 41, likes to go on walks, and is a Democrat."] <- 1
















#####  Cleaning Study 2

########
######## Loading data2 and initial cleaning
########




#### load 'data2'

data2 <- read_excel("data2.xlsx")

data2 <- data2[-c(1, 2),]

# Limit data2 to those who finished the survey (22 didn't)

data2$finished <- as.factor(data2$Finished)

data2 <- data2 %>% filter(finished == "True")

# Exclude out people who don't complete attention checks

data2 <- data2 %>% filter(attention_check_1 == "Red,Green")

data2 <- data2 %>% filter(attention_check_2_2 == "Disagree")

# Exclude duplicate IP addresses

data2 <- data2 %>% group_by(data2$IPAddress) %>%
  filter(n() == 1) %>% 
  ungroup()

### factor, numeric

data2 <- data2 %>% 
  mutate_at(c('feeling_therm_1', 'feeling_therm_2', 'feeling_therm_3', 
              'feeling_therm_4','m_dems_1', 'm_dems_2', 'm_dems_3', 'm_dems_4', 'm_dems_5', 'm_dems_6',
              'm_reps_1', 'm_reps_2', 'm_reps_3', 'm_reps_4', 'm_reps_5', 'm_reps_6',
              'age'), as.numeric)

data2 <- data2 %>% 
  mutate_at(c('partisanship', 'gender', 'lean',
              'strong_rep', 'strong_dem', 'ard_reps_morefewer10', 'ard_dems_morefewer10',
              'ard_reps_more10', 'ard_reps_fewer10', 
              'ard_dems_more10', 'ard_dems_fewer10',
              'race', 'hispanic', 'education', 'religion', 
              'ideology', 'state', 'treatment_1', 'treatment_2', 'treatment_3', 
              'treatment_4', 'Random.Block.1', 'FL_38_DO', 'political_interest'), as.factor)

# Age delete mistakes
data2$age[data2$age == "-3"] <- NA
data2$age[data2$age == "0.47"] <- NA
data2$age[data2$age == "17"] <- NA
data2$age[data2$age == "16866"] <- NA
data2$age[data2$age == "28278"] <- NA
data2$age[data2$age == "1984"] <- NA

data2$Age <- data2$age

#Education
data2$Education <- ifelse(data2$education == "College degree" | data2$education == "Some graduate school" |
                            data2$education == "Graduate school" | data2$education == "Some college", 
                          "Some College", "No College")

data2$Education <- as.factor(data2$Education)
data2$Education <- relevel(data2$Education, ref = "No College")

# Race

data2$Hispanic <- data2$hispanic

data2$Race <- NA
data2$Race[data2$race == "White" & data2$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic White"
data2$Race[data2$race == "Asian" & data2$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic Asian"
data2$Race[data2$race == "Black or African American" & data2$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic Black"
data2$Race[data2$race == "American Indian or Alaska Native" | 
             data2$race == "Native Hawaiian or Pacific Islander" |
             data2$race == "Some other race" 
           & data2$hispanic == "Not Hispanic or Latino"] <- "non-Hispanic Some Other race"
data2$Race[data2$hispanic == "Hispanic or Latino"] <- "Hispanic any race"

data2$Race <- as.factor(data2$Race)
data2$Race <- relevel(data2$Race, ref = "non-Hispanic White")

# Ideology

data2$ideology[data2$ideology == ""] <-  NA
data2$ideology <- factor(data2$ideology, levels = c("Slightly liberal", "Liberal", "Extremely liberal",
                                                    "Moderate; middle of the road", "Slightly conservative", "Conservative",
                                                    "Extremely conservative"))

data2$ideology <- relevel(data2$ideology, ref = "Moderate; middle of the road")

data2$Ideology <- data2$ideology

#polinterest
data2$Political_interest <- as.numeric(data2$political_interest)

# gender
data2$Gender <- ifelse(data2$gender == "Male", "Male", "Female/non-Binary")
data2$Gender <- as.factor(data2$Gender)
data2$Gender <- relevel(data2$Gender, ref = "Female/non-Binary")

## Make new variable for Democrat/Republican, including leaners

data2$all_partisanship <- NA
data2$all_partisanship[data2$partisanship == "Democrat" | data2$lean == "Closer to the Democratic Party"] <- "Democrat"
data2$all_partisanship[data2$partisanship == "Republican" | data2$lean == "Closer to the Republican Party"] <- "Republican"

# Make partisanship variable that distinguishes between dems and close dems

data2$partisanship_detailed <- NA
data2$partisanship_detailed[data2$partisanship == "Democrat"] <- "Democrat" 
data2$partisanship_detailed[data2$partisanship == "Republican"] <- "Republican" 
data2$partisanship_detailed[data2$lean == "Closer to the Democratic Party"] <- "Close dem"
data2$partisanship_detailed[data2$lean == "Closer to the Republican Party"] <- "Close rep"

# Dummy for strong partisanship

data2$strong_dem[data2$strong_dem == ""] <- NA
data2$strong_rep[data2$strong_rep == ""] <- NA

data2$Strong_partisan <- NA
data2$Strong_partisan[data2$strong_rep == "Strong" | data2$strong_dem == "Strong"] <- 1
data2$Strong_partisan[data2$strong_rep == "Not very strong" | data2$strong_dem == "Not very strong"] <- 0
data2$Strong_partisan[data2$partisanship_detailed == "Close dem" | data2$partisanship_detailed == "Close rep"] <- 0



###### 
###### Construct main variables for analysis
######

### Independent variables
data2$ard_LB <- NA

# transform categories into numbers. 

data2$dems_fewer10 <- NA
data2$dems_fewer10[data2$ard_dems_fewer10 == "0"] <- 0
data2$dems_fewer10[data2$ard_dems_fewer10 == "1-2"] <- 1
data2$dems_fewer10[data2$ard_dems_fewer10 == "3-5"] <- 3
data2$dems_fewer10[data2$ard_dems_fewer10 == "6-9"] <- 6

data2$dems_more10 <- NA
data2$dems_more10[data2$ard_dems_more10 == "10-12"] <- 10
data2$dems_more10[data2$ard_dems_more10 == "13-20"] <- 13
data2$dems_more10[data2$ard_dems_more10 == "21-50"] <- 21
data2$dems_more10[data2$ard_dems_more10 == "more than 50"] <- 50

data2$reps_fewer10 <- NA
data2$reps_fewer10[data2$ard_reps_fewer10 == "0"] <- 0
data2$reps_fewer10[data2$ard_reps_fewer10 == "1 - 2"] <- 1
data2$reps_fewer10[data2$ard_reps_fewer10 == "3 - 5"] <- 3
data2$reps_fewer10[data2$ard_reps_fewer10 == "6 - 9"] <- 6

data2$reps_more10 <- NA
data2$reps_more10[data2$ard_reps_more10 == "10 - 12"] <- 10
data2$reps_more10[data2$ard_reps_more10 == "13 - 20"] <- 13
data2$reps_more10[data2$ard_reps_more10 == "21 - 50"] <- 21
data2$reps_more10[data2$ard_reps_more10 == "more than 50"] <- 50

# Make new variable for all reps and all dems

data2$ardLB_allreps <- ifelse(is.na(data2$reps_fewer10) & is.na(data2$reps_more10), 
                              NA, 
                              rowSums(data2[, c("reps_fewer10", "reps_more10")], na.rm = TRUE))


data2$ardLB_alldems <- ifelse(is.na(data2$dems_fewer10) & is.na(data2$dems_more10), 
                              NA, 
                              rowSums(data2[, c("dems_fewer10", "dems_more10")], na.rm = TRUE))

# Make variable of outparty ties 

data2$ardLB_outpartyties <- NA
data2$ardLB_outpartyties[data2$all_partisanship == "Democrat"] <- 
  data2$ardLB_allreps[data2$all_partisanship == "Democrat"]
data2$ardLB_outpartyties[data2$all_partisanship == "Republican"] <- 
  data2$ardLB_alldems[data2$all_partisanship == "Republican"]
data2$ardLB_outpartyties[data2$ard_reps_fewer10 == "0" & 
                           data2$ard_dems_fewer10 == "0"] <- NA

data2$ardLB_inpartyties <- NA
data2$ardLB_inpartyties[data2$all_partisanship == "Democrat"] <- 
  data2$ardLB_alldems[data2$all_partisanship == "Democrat"]
data2$ardLB_inpartyties[data2$all_partisanship == "Republican"] <- 
  data2$ardLB_allreps[data2$all_partisanship == "Republican"]
data2$ardLB_inpartyties[data2$ard_reps_fewer10 == "0" & 
                          data2$ard_dems_fewer10 == "0"] <- NA

# Make variable of whole network

data2$ardLB_wholenetwork <- ifelse(is.na(data2$ardLB_allreps) & is.na(data2$ardLB_alldems),
                                   NA,
                                   rowSums(data2[,c("ardLB_allreps", "ardLB_alldems")], na.rm = TRUE)) 

# Make ard variable: proportion of outparty ties

data2$ardLB_including_isolated_people <- ifelse(data2$ardLB_outpartyties == 0 & data2$ardLB_wholenetwork == 0, 999,
                                                ifelse(data2$ardLB_inpartyties == 0 & data2$ardLB_outpartyties > 0, 1,
                                                       ifelse(data2$ardLB_outpartyties == 0 & data2$ardLB_inpartyties > 0, 0,
                                                              data2$ardLB_outpartyties / data2$ardLB_wholenetwork)))

data2$ard_LB <- ifelse(data2$ardLB_outpartyties == 0 & data2$ardLB_wholenetwork == 0, NA,
                       ifelse(data2$ardLB_inpartyties == 0 & data2$ardLB_outpartyties > 0, 1,
                              ifelse(data2$ardLB_outpartyties == 0 & data2$ardLB_inpartyties > 0, 0,
                                     data2$ardLB_outpartyties / data2$ardLB_wholenetwork)))

data2$Acquaintance_heterogeneity <- data2$ard_LB 

# Make dummy for graphs


data2$ardLB_dummy <- ifelse(data2$ard_LB > 0.32, 1, 0) 
data2$Acquaintance_dummy <- data2$ardLB_dummy
levels(data2$Acquaintance_dummy) <- c("Homogeneous", "Heterogeneous")


data2$ardLB_outpartyties_dummy <- ifelse(data2$ardLB_outpartyties > 8.4, 1, 0) 


########
######## Dependent variables
########

## 1.1 Feeling thermometer: outparty

data2$outparty_affect <- NA

data2$outparty_affect[data2$all_partisanship == "Democrat"] <- data2$feeling_therm_2[data2$all_partisanship == "Democrat"]  
data2$outparty_affect[data2$all_partisanship == "Republican"] <- data2$feeling_therm_1[data2$all_partisanship == "Republican"]  

data2$outpartisan_affect <- NA

data2$outpartisan_affect[data2$all_partisanship == "Democrat"] <- data2$feeling_therm_4[data2$all_partisanship == "Democrat"]  
data2$outpartisan_affect[data2$all_partisanship == "Republican"] <- data2$feeling_therm_3[data2$all_partisanship == "Republican"]  

## 1.2 Feeling thermometer: inparty

data2$inparty_affect <- NA

data2$inparty_affect[data2$all_partisanship == "Democrat"] <- data2$feeling_therm_1[data2$all_partisanship == "Democrat"]  
data2$inparty_affect[data2$all_partisanship == "Republican"] <- data2$feeling_therm_2[data2$all_partisanship == "Republican"]  

data2$inpartisan_affect <- NA

data2$inpartisan_affect[data2$all_partisanship == "Democrat"] <- data2$feeling_therm_3[data2$all_partisanship == "Democrat"]  
data2$inpartisan_affect[data2$all_partisanship == "Republican"] <- data2$feeling_therm_4[data2$all_partisanship == "Republican"]  

## Index it

data2 <- data2 %>% mutate(outpartisan_affect = (outpartisan_affect/100))
data2 <- data2 %>% mutate(inpartisan_affect = (inpartisan_affect/100))
data2 <- data2 %>% mutate(outparty_affect = (outparty_affect/100))
data2 <- data2 %>% mutate(inparty_affect = (inparty_affect/100))

data2 <- data2 %>% drop_na(trait_reps_1, trait_reps_2, trait_reps_3, trait_reps_4, trait_reps_5,
                           trait_reps_6, trait_reps_7, trait_reps_8, trait_dems_1, trait_dems_2, 
                           trait_dems_3, trait_dems_4, trait_dems_5, trait_dems_6, trait_dems_7, 
                           trait_dems_8, trust_dems, trust_reps)
### 2. Trait ratings

# Inparty trait rating

data2$inparty_american <- NA
data2$inparty_american[data2$all_partisanship == "Democrat"] <- data2$trait_dems_1[data2$all_partisanship == "Democrat"] 
data2$inparty_american[data2$all_partisanship == "Republican"] <- data2$trait_reps_1[data2$all_partisanship == "Republican"] 

data2$inparty_intelligent <- NA
data2$inparty_intelligent[data2$all_partisanship == "Democrat"] <- data2$trait_dems_2[data2$all_partisanship == "Democrat"] 
data2$inparty_intelligent[data2$all_partisanship == "Republican"] <- data2$trait_reps_2[data2$all_partisanship == "Republican"] 

data2$inparty_honest <- NA
data2$inparty_honest[data2$all_partisanship == "Democrat"] <- data2$trait_dems_3[data2$all_partisanship == "Democrat"] 
data2$inparty_honest[data2$all_partisanship == "Republican"] <- data2$trait_reps_3[data2$all_partisanship == "Republican"] 

data2$inparty_openminded <- NA
data2$inparty_openminded[data2$all_partisanship == "Democrat"] <- data2$trait_dems_4[data2$all_partisanship == "Democrat"] 
data2$inparty_openminded[data2$all_partisanship == "Republican"] <- data2$trait_reps_4[data2$all_partisanship == "Republican"] 

data2$inparty_generous <- NA
data2$inparty_generous[data2$all_partisanship == "Democrat"] <- data2$trait_dems_5[data2$all_partisanship == "Democrat"] 
data2$inparty_generous[data2$all_partisanship == "Republican"] <- data2$trait_reps_5[data2$all_partisanship == "Republican"] 

data2$inparty_hypocritical <- NA
data2$inparty_hypocritical[data2$all_partisanship == "Democrat"] <- data2$trait_dems_6[data2$all_partisanship == "Democrat"] 
data2$inparty_hypocritical[data2$all_partisanship == "Republican"] <- data2$trait_reps_6[data2$all_partisanship == "Republican"] 

data2$inparty_selfish <- NA
data2$inparty_selfish[data2$all_partisanship == "Democrat"] <- data2$trait_dems_7[data2$all_partisanship == "Democrat"] 
data2$inparty_selfish[data2$all_partisanship == "Republican"] <- data2$trait_reps_7[data2$all_partisanship == "Republican"] 

data2$inparty_mean <- NA
data2$inparty_mean[data2$all_partisanship == "Democrat"] <- data2$trait_dems_8[data2$all_partisanship == "Democrat"] 
data2$inparty_mean[data2$all_partisanship == "Republican"] <- data2$trait_reps_8[data2$all_partisanship == "Republican"] 

# Outparty trait rating

data2$outparty_american <- NA
data2$outparty_american[data2$all_partisanship == "Democrat"] <- data2$trait_reps_1[data2$all_partisanship == "Democrat"] 
data2$outparty_american[data2$all_partisanship == "Republican"] <- data2$trait_dems_1[data2$all_partisanship == "Republican"] 

data2$outparty_intelligent <- NA
data2$outparty_intelligent[data2$all_partisanship == "Democrat"] <- data2$trait_reps_2[data2$all_partisanship == "Democrat"] 
data2$outparty_intelligent[data2$all_partisanship == "Republican"] <- data2$trait_dems_2[data2$all_partisanship == "Republican"] 

data2$outparty_honest <- NA
data2$outparty_honest[data2$all_partisanship == "Democrat"] <- data2$trait_reps_3[data2$all_partisanship == "Democrat"] 
data2$outparty_honest[data2$all_partisanship == "Republican"] <- data2$trait_dems_3[data2$all_partisanship == "Republican"] 

data2$outparty_openminded <- NA
data2$outparty_openminded[data2$all_partisanship == "Democrat"] <- data2$trait_reps_4[data2$all_partisanship == "Democrat"] 
data2$outparty_openminded[data2$all_partisanship == "Republican"] <- data2$trait_dems_4[data2$all_partisanship == "Republican"] 

data2$outparty_generous <- NA
data2$outparty_generous[data2$all_partisanship == "Democrat"] <- data2$trait_reps_5[data2$all_partisanship == "Democrat"] 
data2$outparty_generous[data2$all_partisanship == "Republican"] <- data2$trait_dems_5[data2$all_partisanship == "Republican"] 

data2$outparty_hypocritical <- NA
data2$outparty_hypocritical[data2$all_partisanship == "Democrat"] <- data2$trait_reps_6[data2$all_partisanship == "Democrat"] 
data2$outparty_hypocritical[data2$all_partisanship == "Republican"] <- data2$trait_dems_6[data2$all_partisanship == "Republican"] 

data2$outparty_selfish <- NA
data2$outparty_selfish[data2$all_partisanship == "Democrat"] <- data2$trait_reps_7[data2$all_partisanship == "Democrat"] 
data2$outparty_selfish[data2$all_partisanship == "Republican"] <- data2$trait_dems_7[data2$all_partisanship == "Republican"] 

data2$outparty_mean <- NA
data2$outparty_mean[data2$all_partisanship == "Democrat"] <- data2$trait_reps_8[data2$all_partisanship == "Democrat"] 
data2$outparty_mean[data2$all_partisanship == "Republican"] <- data2$trait_dems_8[data2$all_partisanship == "Republican"] 

## Recode all to make numerical and to have positive values correspond to a warmer rating

data2 <- data2 %>% mutate(inparty_american = case_when(
  inparty_american == "Not at all well" ~ 1,
  inparty_american == "Not too well" ~ 2,
  inparty_american == "Somewhat well" ~ 3,
  inparty_american == "Very well" ~ 4,
  inparty_american == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(inparty_intelligent = case_when(
  inparty_intelligent == "Not at all well" ~ 1,
  inparty_intelligent == "Not too well" ~ 2,
  inparty_intelligent == "Somewhat well" ~ 3,
  inparty_intelligent == "Very well" ~ 4,
  inparty_intelligent == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(inparty_honest = case_when(
  inparty_honest == "Not at all well" ~ 1,
  inparty_honest == "Not too well" ~ 2,
  inparty_honest == "Somewhat well" ~ 3,
  inparty_honest == "Very well" ~ 4,
  inparty_honest == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(inparty_openminded = case_when(
  inparty_openminded == "Not at all well" ~ 1,
  inparty_openminded == "Not too well" ~ 2,
  inparty_openminded == "Somewhat well" ~ 3,
  inparty_openminded == "Very well" ~ 4,
  inparty_openminded == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(inparty_generous = case_when(
  inparty_generous == "Not at all well" ~ 1,
  inparty_generous == "Not too well" ~ 2,
  inparty_generous == "Somewhat well" ~ 3,
  inparty_generous == "Very well" ~ 4,
  inparty_generous == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(inparty_hypocritical = case_when(
  inparty_hypocritical == "Not at all well" ~ 5,
  inparty_hypocritical == "Not too well" ~ 4,
  inparty_hypocritical == "Somewhat well" ~ 3,
  inparty_hypocritical == "Very well" ~ 2,
  inparty_hypocritical == "Extremely well" ~ 1))

data2 <- data2 %>% mutate(inparty_selfish = case_when(
  inparty_selfish == "Not at all well" ~ 5,
  inparty_selfish == "Not too well" ~ 4,
  inparty_selfish == "Somewhat well" ~ 3,
  inparty_selfish == "Very well" ~ 2,
  inparty_selfish == "Extremely well" ~ 1))

data2 <- data2 %>% mutate(inparty_mean = case_when(
  inparty_mean == "Not at all well" ~ 5,
  inparty_mean == "Not too well" ~ 4,
  inparty_mean == "Somewhat well" ~ 3,
  inparty_mean == "Very well" ~ 2,
  inparty_mean == "Extremely well" ~ 1))

data2 <- data2 %>% mutate(outparty_american = case_when(
  outparty_american == "Not at all well" ~ 1,
  outparty_american == "Not too well" ~ 2,
  outparty_american == "Somewhat well" ~ 3,
  outparty_american == "Very well" ~ 4,
  outparty_american == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(outparty_intelligent = case_when(
  outparty_intelligent == "Not at all well" ~ 1,
  outparty_intelligent == "Not too well" ~ 2,
  outparty_intelligent == "Somewhat well" ~ 3,
  outparty_intelligent == "Very well" ~ 4,
  outparty_intelligent == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(outparty_honest = case_when(
  outparty_honest == "Not at all well" ~ 1,
  outparty_honest == "Not too well" ~ 2,
  outparty_honest == "Somewhat well" ~ 3,
  outparty_honest == "Very well" ~ 4,
  outparty_honest == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(outparty_openminded = case_when(
  outparty_openminded == "Not at all well" ~ 1,
  outparty_openminded == "Not too well" ~ 2,
  outparty_openminded == "Somewhat well" ~ 3,
  outparty_openminded == "Very well" ~ 4,
  outparty_openminded == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(outparty_generous = case_when(
  outparty_generous == "Not at all well" ~ 1,
  outparty_generous == "Not too well" ~ 2,
  outparty_generous == "Somewhat well" ~ 3,
  outparty_generous == "Very well" ~ 4,
  outparty_generous == "Extremely well" ~ 5))

data2 <- data2 %>% mutate(outparty_hypocritical = case_when(
  outparty_hypocritical == "Not at all well" ~ 5,
  outparty_hypocritical == "Not too well" ~ 4,
  outparty_hypocritical == "Somewhat well" ~ 3,
  outparty_hypocritical == "Very well" ~ 2,
  outparty_hypocritical == "Extremely well" ~ 1))

data2 <- data2 %>% mutate(outparty_selfish = case_when(
  outparty_selfish == "Not at all well" ~ 5,
  outparty_selfish == "Not too well" ~ 4,
  outparty_selfish == "Somewhat well" ~ 3,
  outparty_selfish == "Very well" ~ 2,
  outparty_selfish == "Extremely well" ~ 1))

data2 <- data2 %>% mutate(outparty_mean = case_when(
  outparty_mean == "Not at all well" ~ 5,
  outparty_mean == "Not too well" ~ 4,
  outparty_mean == "Somewhat well" ~ 3,
  outparty_mean == "Very well" ~ 2,
  outparty_mean == "Extremely well" ~ 1))

## Index outparty

data2 <- data2 %>% mutate(outparty_american = (outparty_american-1)/(5-1))
data2 <- data2 %>% mutate(outparty_intelligent = (outparty_intelligent-1)/(5-1))
data2 <- data2 %>% mutate(outparty_honest = (outparty_honest-1)/(5-1))
data2 <- data2 %>% mutate(outparty_openminded = (outparty_openminded-1)/(5-1))
data2 <- data2 %>% mutate(outparty_generous = (outparty_generous-1)/(5-1))
data2 <- data2 %>% mutate(outparty_hypocritical = (outparty_hypocritical-1)/(5-1))
data2 <- data2 %>% mutate(outparty_selfish = (outparty_selfish-1)/(5-1))
data2 <- data2 %>% mutate(outparty_mean = (outparty_mean-1)/(5-1))

## Index inparty

data2 <- data2 %>% mutate(inparty_american = (inparty_american-1)/(5-1))
data2 <- data2 %>% mutate(inparty_intelligent = (inparty_intelligent-1)/(5-1))
data2 <- data2 %>% mutate(inparty_honest = (inparty_honest-1)/(5-1))
data2 <- data2 %>% mutate(inparty_openminded = (inparty_openminded-1)/(5-1))
data2 <- data2 %>% mutate(inparty_generous = (inparty_generous-1)/(5-1))
data2 <- data2 %>% mutate(inparty_hypocritical = (inparty_hypocritical-1)/(5-1))
data2 <- data2 %>% mutate(inparty_selfish = (inparty_selfish-1)/(5-1))
data2 <- data2 %>% mutate(inparty_mean = (inparty_mean-1)/(5-1))

## total outparty and inparty rating

data2 <- data2 %>% 
  mutate(outparty_traits = (outparty_american + outparty_intelligent +
                              outparty_honest + outparty_openminded + outparty_generous +
                              outparty_hypocritical + outparty_selfish + outparty_mean)/8)

data2 <- data2 %>% 
  mutate(inparty_traits = (inparty_american + inparty_intelligent +
                             inparty_honest + inparty_openminded + inparty_generous +
                             inparty_hypocritical + inparty_selfish + inparty_mean)/8)

## 3 Trust

data2$inparty_trust <- NA
data2$inparty_trust[data2$all_partisanship == "Democrat"] <- data2$trust_dems[data2$all_partisanship == "Democrat"] 
data2$inparty_trust[data2$all_partisanship == "Republican"] <- data2$trust_reps[data2$all_partisanship == "Republican"] 

data2$outparty_trust <- NA
data2$outparty_trust[data2$all_partisanship == "Democrat"] <- data2$trust_reps[data2$all_partisanship == "Democrat"] 
data2$outparty_trust[data2$all_partisanship == "Republican"] <- data2$trust_dems[data2$all_partisanship == "Republican"] 

data2 <- data2 %>% mutate(inparty_trust = case_when(
  inparty_trust == "Almost always" ~ 5,
  inparty_trust == "Most of the time" ~ 4,
  inparty_trust == "About half the time" ~ 3,
  inparty_trust == "Once in a while" ~ 2,
  inparty_trust == "Almost never" ~ 1))

data2 <- data2 %>% mutate(outparty_trust = case_when(
  outparty_trust == "Almost always" ~ 5,
  outparty_trust == "Most of the time" ~ 4,
  outparty_trust == "About half the time" ~ 3,
  outparty_trust == "Once in a while" ~ 2,
  outparty_trust == "Almost never" ~ 1))

## Index

data2 <- data2 %>% mutate(outparty_trust = (outparty_trust-1)/(5-1))
data2 <- data2 %>% mutate(inparty_trust = (inparty_trust-1)/(5-1))

## Make one scale (0-1) for outparty feelings 

data2 <- data2 %>% 
  mutate(outparty_final = (outpartisan_affect + outparty_american + outparty_intelligent +
                             outparty_honest + outparty_openminded + outparty_generous +
                             outparty_hypocritical + outparty_selfish + outparty_mean + 
                             outparty_trust)/10)

data2 <- data2 %>% 
  mutate(inparty_final = (inpartisan_affect + inparty_american + inparty_intelligent +
                            inparty_honest + inparty_openminded + inparty_generous +
                            inparty_hypocritical + inparty_selfish + inparty_mean + 
                            inparty_trust)/10)

# Dummy

data2$outparty_final_dummy <- ifelse(data2$outparty_final < 0.34, 1, 0)

# Inparty minus outparty

data2 <- data2 %>%
  mutate(AP_final = ((inpartisan_affect - outpartisan_affect) + (inparty_american - outparty_american) +
                       (inparty_intelligent - outparty_intelligent) + (inparty_honest - outparty_honest) +
                       (inparty_openminded - outparty_openminded) + (inparty_generous - outparty_generous) +
                       (inparty_hypocritical - outparty_hypocritical) + (inparty_selfish - outparty_selfish) +
                       (inparty_mean - outparty_mean) + (inparty_trust - outparty_trust))/10) 


#### Treatment variable for experiment

data2$Treatment <- NA
data2$Treatment[data2$FL_38_DO == "FL_39"] <- "Treatment"
data2$Treatment[data2$FL_38_DO == "FL_40"] <- "Control"
data2$Treatment <- as.factor(data2$Treatment)

## Perception of acquaintances as different or similar
data2 <- data2 %>% mutate(t_income = case_when(
  treatment_1 == "Very similar" ~ 2,
  treatment_1 == "Quite similar" ~ 1,
  treatment_1 == "Quite different" ~ -1,
  treatment_1 == "Very different" ~ -2))

data2 <- data2 %>% mutate(t_polinterest = case_when(
  treatment_2 == "Very similar" ~ 2,
  treatment_2 == "Quite similar" ~ 1,
  treatment_2 == "Quite different" ~ -1,
  treatment_2 == "Very different" ~ -2))

data2 <- data2 %>% mutate(t_poldiscussion = case_when(
  treatment_3 == "Very similar" ~ 2,
  treatment_3 == "Quite similar" ~ 1,
  treatment_3 == "Quite different" ~ -1,
  treatment_3 == "Very different" ~ -2))

data2 <- data2 %>% mutate(t_countrylove = case_when(
  treatment_4 == "Very similar" ~ 2,
  treatment_4 == "Quite similar" ~ 1,
  treatment_4 == "Quite different" ~ -1,
  treatment_4 == "Very different" ~ -2))

## Total similarity score

data2 <- data2 %>% mutate(total_similarity = t_income + t_polinterest +
                            t_poldiscussion + t_countrylove)

# Dummy that takes 1 if it's more similar, 0 if it's more different

data2$total_similarity_dummy <- ifelse(data2$total_similarity == 0, NA,
                                       ifelse(data2$total_similarity > 0, 1, 0))

# Dummy that takes one if it's more different or the same

data2$total_similarity_dummy_2 <- ifelse(data2$total_similarity > -1, 1, 0)

#### Misperceptions

# Benchmarks

data2$union_benchmark <- 10.5
data2$lgbt_benchmark <- 6.3
data2$black_benchmark <- 24
data2$atheist_benchmark <- 8.7
data2$dem_talk_benchmark <- 24
data2$dem_moderate_benchmark <- 50

data2$south_benchmark <- 35.7
data2$sixtyfive_benchmark <- 21.3
data2$evangelical_benchmark <- 34
data2$rich_benchmark <- 2.2
data2$rep_talk_benchmark <- 28
data2$rep_moderate_benchmark <- 33

## Misperceptions

data2$union_mis <- data2$m_dems_1 - data2$union_benchmark
data2$lgbt_mis <- data2$m_dems_2 - data2$lgbt_benchmark
data2$black_mis <- data2$m_dems_3 - data2$black_benchmark
data2$atheist_mis <- data2$m_dems_4 - data2$atheist_benchmark
data2$dem_talk_mis <- data2$m_dems_5 - data2$dem_talk_benchmark
data2$dem_moderate_mis <- data2$m_dems_6 - data2$dem_moderate_benchmark
data2$south_mis <- data2$m_reps_1 - data2$south_benchmark
data2$sixtyfive_mis <- data2$m_reps_2 - data2$sixtyfive_benchmark
data2$evangelical_mis <- data2$m_reps_3 - data2$evangelical_benchmark
data2$rich_mis <- data2$m_reps_4 - data2$rich_benchmark
data2$rep_talk_mis <- data2$m_reps_5 - data2$rep_talk_benchmark
data2$rep_moderate_mis <- data2$m_reps_6 - data2$rep_moderate_benchmark

## Total misperceptions, irrespective of partisanship

data2$total_misperceptions <- rowSums(data2[, c("union_mis", "lgbt_mis", "black_mis",
                                                "atheist_mis", "dem_talk_mis", "dem_moderate_mis",
                                                "south_mis", "sixtyfive_mis", "evangelical_mis",
                                                "rich_mis", "rep_talk_mis", "rep_moderate_mis")], na.rm = TRUE)

## Total misperceptions, ahler and sood 2018 variables, irrespective of partisanship 

data2$total_misperceptions_AS2018 <- rowSums(data2[, c("union_mis", "lgbt_mis", "black_mis",
                                                       "atheist_mis", 
                                                       "south_mis", "sixtyfive_mis", "evangelical_mis",
                                                       "rich_mis")], na.rm = TRUE)

# Total misperceptions (including moderation and discussion)

selected_cols <- c("union_mis", "lgbt_mis", "black_mis", "atheist_mis", "dem_talk_mis",
                   "dem_moderate_mis", "south_mis", "sixtyfive_mis", "evangelical_mis",
                   "rich_mis", "rep_talk_mis", "rep_moderate_mis")

data2$total_misperceptions <- apply(data2[selected_cols], 1, function(row) {
  if (all(is.na(row))) {
    NA
  } else {
    sum(row, na.rm = TRUE)
  }
})

# Total misperceptions (only ahler and sood)

selected_cols <- c("union_mis", "lgbt_mis", "black_mis", "atheist_mis","south_mis", 
                   "sixtyfive_mis", "evangelical_mis", "rich_mis")

data2$total_misperceptions_AS2018 <- apply(data2[selected_cols], 1, function(row) {
  if (all(is.na(row))) {
    NA
  } else {
    sum(row, na.rm = TRUE)
  }
})

## Total misperceptions towards Republicans 

selected_cols <- c("south_mis", "sixtyfive_mis", "evangelical_mis",
                   "rich_mis", "rep_talk_mis", "rep_moderate_mis")

data2$rep_total <- apply(data2[selected_cols], 1, function(row) {
  if (all(is.na(row))) {
    NA
  } else {
    sum(row, na.rm = TRUE)
  }
})

# Total misperceptions towards democrats

selected_cols <- c("union_mis", "lgbt_mis", "black_mis", "atheist_mis", "dem_talk_mis",
                   "dem_moderate_mis")

data2$dem_total <- apply(data2[selected_cols], 1, function(row) {
  if (all(is.na(row))) {
    NA
  } else {
    sum(row, na.rm = TRUE)
  }
})

## Rep total Ahler sood

selected_cols <- c("south_mis", "sixtyfive_mis", "evangelical_mis",
                   "rich_mis")

data2$rep_total_AS2018 <- apply(data2[selected_cols], 1, function(row) {
  if (all(is.na(row))) {
    NA
  } else {
    sum(row, na.rm = TRUE)
  }
})

# Dem total Ahler Sood

selected_cols <- c("union_mis", "lgbt_mis", "black_mis", "atheist_mis")

data2$dem_total_AS2018 <- apply(data2[selected_cols], 1, function(row) {
  if (all(is.na(row))) {
    NA
  } else {
    sum(row, na.rm = TRUE)
  }
})

## Variable for total misperceptions (following Ahler and Sood)

data2$mean_error_union <- (data2$m_dems_1 - 10.5)/10.5 
data2$mean_error_lgbt <- (data2$m_dems_2 - 6.3)/6.3 
data2$mean_error_black <- (data2$m_dems_3 - 24)/24 
data2$mean_error_aa <- (data2$m_dems_4 - 8.7)/8.7 
data2$mean_error_dem_talk <- (data2$m_dems_5 - 24)/24
data2$mean_error_dem_ideo <- (50 - data2$m_dems_6)/50
data2$mean_error_south <- (data2$m_reps_1 - 35.7)/35.7 
data2$mean_error_sixtyfive <- (data2$m_reps_2 - 21.3)/21.3 
data2$mean_error_evangelican <- (data2$m_reps_3 - 34)/34 
data2$mean_error_rich <- (data2$m_reps_4 - 2.2)/2.2 
data2$mean_error_rep_talk <- (data2$m_reps_5 - 28)/28
data2$mean_error_rep_ideo <- (33 - data2$m_reps_6)/33

data2$mean_error_dem <- rowMeans(data2[, c("mean_error_aa", "mean_error_black", "mean_error_lgbt", "mean_error_union", 
                                           "mean_error_dem_talk", "mean_error_dem_ideo")], na.rm = TRUE)
data2$mean_error_rep <- rowMeans(data2[, c("mean_error_south", "mean_error_sixtyfive", "mean_error_evangelican", "mean_error_rich", 
                                           "mean_error_rep_talk", "mean_error_rep_ideo")], na.rm = TRUE)
data2$mean_error_all <- rowMeans(data2[, c("mean_error_aa", "mean_error_black", "mean_error_lgbt", "mean_error_union", 
                                           "mean_error_south", "mean_error_sixtyfive", "mean_error_evangelican", 
                                           "mean_error_rich", "mean_error_dem_talk", "mean_error_dem_ideo", "mean_error_rep_talk", 
                                           "mean_error_rep_ideo")], na.rm = TRUE)

data2$mean_error_dem_AS <- rowMeans(data2[, c("mean_error_aa", "mean_error_black", "mean_error_lgbt", "mean_error_union")], na.rm = TRUE)
data2$mean_error_rep_AS <- rowMeans(data2[, c("mean_error_south", "mean_error_sixtyfive", "mean_error_evangelican", "mean_error_rich")], na.rm = TRUE)
data2$mean_error_all_AS <- rowMeans(data2[, c("mean_error_aa", "mean_error_black", "mean_error_lgbt", "mean_error_union",
                                              "mean_error_south", "mean_error_sixtyfive", "mean_error_evangelican", "mean_error_rich")], na.rm = TRUE)

## Variable for only in- and outparty misperceptions (following Ahler and Sood)

data2$mean_error_outparty <- NA
data2$mean_error_outparty[data2$all_partisanship == "Democrat"] <- data2$mean_error_rep[data2$all_partisanship == "Democrat"]
data2$mean_error_outparty[data2$all_partisanship == "Republican"] <- data2$mean_error_dem[data2$all_partisanship == "Republican"]

data2$mean_error_outparty_AS <- NA
data2$mean_error_outparty_AS[data2$all_partisanship == "Democrat"] <- data2$mean_error_rep_AS[data2$all_partisanship == "Democrat"]
data2$mean_error_outparty_AS[data2$all_partisanship == "Republican"] <- data2$mean_error_dem_AS[data2$all_partisanship == "Republican"]

# Inparty

data2$mean_error_inparty <- NA
data2$mean_error_inparty[data2$all_partisanship == "Democrat"] <- data2$mean_error_dem[data2$all_partisanship == "Democrat"]
data2$mean_error_inparty[data2$all_partisanship == "Republican"] <- data2$mean_error_rep[data2$all_partisanship == "Republican"]

data2$mean_error_inparty_AS <- NA
data2$mean_error_inparty_AS[data2$all_partisanship == "Democrat"] <- data2$mean_error_dem_AS[data2$all_partisanship == "Democrat"]
data2$mean_error_inparty_AS[data2$all_partisanship == "Republican"] <- data2$mean_error_rep_AS[data2$all_partisanship == "Republican"]






### Move on to the the 'Analysis' datafile

